package com.example.demo.controller;

import com.example.demo.job.DynamicScheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping("/tasks")
public class TaskController {

    @Autowired
    private DynamicScheduler dynamicScheduler;

    // 添加/更新任务
    @PostMapping("/{taskId}")
    public String addOrUpdateTask(
            @PathVariable String taskId,
            @RequestParam String cron,
            @RequestBody String taskContent) {
        
//        dynamicScheduler.addOrUpdateTask(taskId, () -> {
//            // 执行任务逻辑
//            System.out.println("执行任务: " + taskId + ", 内容: " + taskContent);
//        }, cron);
        
        return "任务 " + taskId + " 已添加/更新";
    }

    // 删除任务
    @DeleteMapping("/{taskId}")
    public String cancelTask(@PathVariable String taskId) {
        dynamicScheduler.cancelTask(taskId);
        return "任务 " + taskId + " 已取消";
    }

    // 列出所有任务
    @GetMapping
    public Set<String> listTasks() {
        return dynamicScheduler.getTaskIds();
    }
}